package com.example.flex.controller;

import com.example.flex.domain.UserRoleEntity;
import com.example.flex.service.UserRoleService;
import com.example.flex.utils.common.PageEntity;
import com.example.flex.utils.response.BaseResponse;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * 用户角色关联表控制层
 * @Author: 不是菜鸡爱编程
 * @Date:
 * @Description：
 */
@RestController
@Api(tags = "用户角色关联表接口")
@RequestMapping("userRole")
public class UserRoleController{

    @Autowired
    private UserRoleService userRoleService;

    /**
     * 保存
     *
     * @param userRoleEntity 用户角色关联表
     * @return BaseResponse
     */
    @PostMapping("/save")
    @ApiOperation("保存用户角色关联表")
    public BaseResponse<?> save(@RequestBody @ApiParam("用户角色关联表") UserRoleEntity userRoleEntity){
        boolean saveFlag = userRoleService.save(userRoleEntity);
        if(saveFlag){
            return BaseResponse.success("保存成功");
        }else{
            return BaseResponse.error("保存失败");
        }
    }

    /**
     * 根据主键查询
     *
     * @param id
     * @return BaseResponse
     */
    @GetMapping("/info/{id}")
    @ApiOperation("根据主键查询用户角色关联表")
    public BaseResponse<?> info(@PathVariable @ApiParam("用户角色关联表主键") Integer id){
        return BaseResponse.success(userRoleService.getById(id),"查询成功");
    }

    /**
     * 列表查询
     *
     * @return BaseResponse
     */
    @PostMapping("/list")
    @ApiOperation("列表查询用户角色关联表")
    public BaseResponse<?> list(@RequestBody @ApiParam("用户角色关联表分页参数") PageEntity<UserRoleEntity> userRoleEntityPage){
        UserRoleEntity userRoleEntity = userRoleEntityPage.getData();
        QueryWrapper queryWrapper = getQueryWrapper(userRoleEntity);
        return BaseResponse.success(userRoleService.page(new Page<>(userRoleEntityPage.getPageNum(),userRoleEntityPage.getPageSize()),queryWrapper),"查询成功");
    }

    /**
     * 删除
     *
     * @param ids
     * @return BaseResponse
     */
    @DeleteMapping("/delete/{ids}")
    @ApiOperation("删除用户角色关联表")
    public BaseResponse<?> delete(@PathVariable @ApiParam("用户角色关联表主键(列表)") Integer[] ids){
        return BaseResponse.success(userRoleService.removeByIds(Arrays.asList(ids)),"删除成功");
    }


    /**
     * 修改
     *
     * @param userRoleEntity 用户角色关联表
     * @return BaseResponse
     */
    @PutMapping("/edit")
    @ApiOperation("修改用户角色关联表")
    public BaseResponse<?> edit(@RequestBody @ApiParam("用户角色关联表") UserRoleEntity userRoleEntity){
        return BaseResponse.success(userRoleService.updateById(userRoleEntity),"修改成功");
    }

    /**
     * 获取queryWrapper
     *
     * @param userRoleEntity 用户角色关联表
     * @return {@link QueryWrapper}
     */
    private QueryWrapper getQueryWrapper(UserRoleEntity userRoleEntity){
        return QueryWrapper.create()
                    .eq(UserRoleEntity::getUserId,userRoleEntity.getUserId(),userRoleEntity.getUserId()!=null)
                    .eq(UserRoleEntity::getRoleId,userRoleEntity.getRoleId(),userRoleEntity.getRoleId()!=null);
    }
}